package com.seth.myapplication.surface;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.seth.myapplication.R;
import com.seth.myapplication.dialog.DatePickerFragment;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Seth on 2017/10/11.
 */

public class page2Fragment extends Fragment {
    private static final int REQUEST_DATE = 0;
    private static final String DIALOG_DATE = "DialogDate";

    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img4;

    private Button setDeatTime;
    private ViewGroup.LayoutParams params;
    private Date date;




    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_new_task, container, false);

        initView(view);
        initEvent();


        return view;

    }

    private void initView(View view) {
        img1 = (ImageView) view.findViewById(R.id.task_img1);
        img2 = (ImageView) view.findViewById(R.id.task_img2);
        img3 = (ImageView) view.findViewById(R.id.task_img3);
        img4 = (ImageView) view.findViewById(R.id.task_img4);

        setDeatTime = (Button) view.findViewById(R.id.btn_dead_time);

        date = new Date();
    }

    private void initEvent() {
        initImage(img1);

        setDeatTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getFragmentManager();
                DatePickerFragment dialog = DatePickerFragment.NewInsance(date);
                dialog.setTargetFragment(page2Fragment.this, REQUEST_DATE);
                dialog.show(manager,DIALOG_DATE);
            }
        });

    }

    private void initImage(ImageView img) {
        params =  img.getLayoutParams();
        params.height = 100;
        params.width = 100;
        img.setLayoutParams(params);
        img.setImageResource(R.drawable.ic_add_msg);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void updateDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nDate =format.format(date);
        //nDate = (String) new ("MMMM dd, yyyy h:mmaa",mCrime.getDate());
        setDeatTime.setText(nDate);
    }

    @Override
    public void onActivityResult(int resquestCode, int resultCode, Intent data){
        if(resultCode != Activity.RESULT_OK){
            return ;
        }
        if(resquestCode == REQUEST_DATE){
            Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
            this.date = date;
            updateDate();
        }
    }

}
